using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.Components;
using System.ComponentModel;
using System.Drawing;
using System.Xml;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class holds information on a transformer winding
    /// </summary>
    public class MM_OneLine_TransformerWinding
    {
        #region Variable declarations
        /// <summary>The transformer winding</summary>
        [Category("Data Linkage"), Description("The transformer winding")]
        public MM_Element BaseElement
        {
            get { return _BaseElement; }
            set { _BaseElement = value; }
        }
        private MM_Element _BaseElement;

        /// <summary>The transformer winding</summary>
        [Category("Data Linkage"), Description("The rdf:ID of the transformer winding")]
        public MM_rdfID rdfID
        {
            get { return _rdfID; }
            set { _rdfID = value; }
        }
        private MM_rdfID _rdfID;

        /// <summary>The bounds of the winding within the transformer rectangle</summary>
        [Category("Display"), Description("The bounds of the winding within the transformer rectangle")]
        public Rectangle Bounds
        {
            get { return _Bounds; }
            set { _Bounds = value; }
        }
        private Rectangle _Bounds;


        /// <summary>The Visible of the winding within the transformer rectangle</summary>
        [Category("Display"), Description("Whether the winding is visible"), DefaultValue(true)]
        public bool Visible
        {
            get { return _Visible; }
            set { _Visible = value; }
        }
        private bool _Visible=true;


        /// <summary>Whether the winding is a phase shifter</summary>
        [Category("Display"), Description("Whether the winding is a phase shifter"), DefaultValue(false)]
        public bool IsPhaseShifter
        {
            get { return _IsPhaseShifter; }
            set { _IsPhaseShifter = value; }
        }
        private bool _IsPhaseShifter = false;

        /// <summary>The orientation of the transformer winding</summary>
        [Category("Display"), Description("The orientation of the transformer winding")]
        public MM_OneLine_Element.enumOrientations Orientation
        {
            get { return _Orientation; }
            set { _Orientation = value; }
        }

        /// <summary>The KV Level of the transformer winding</summary>
        [Category("Display"), Description("The KV Level of the transformer winding")]        
        public MM_KVLevel KVLevel
        {
            get
            {
                if (BaseElement == null)
                    return null;
                else
                    return BaseElement.KVLevel; }
        }

        private MM_OneLine_Element.enumOrientations _Orientation;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new transformer winding
        /// </summary>
        /// <param name="BaseElement">The winding associated with this element</param>
        /// <param name="Bounds">The bounds of the element</param>
        /// <param name="Orientation">The orientation of the element</param>
        public MM_OneLine_TransformerWinding(MM_Element BaseElement, Rectangle Bounds, MM_OneLine_Element.enumOrientations Orientation)
        {
            this.BaseElement = BaseElement;
            this.Bounds = Bounds;
            this.Orientation = Orientation;
        }

        /// <summary>
        /// Initialize a new transformer winding
        /// </summary>
        /// <param name="ElemConfig">The XML configuration for the transformer winding</param>
        public MM_OneLine_TransformerWinding(XmlElement ElemConfig)
        {
            if (ElemConfig.HasAttribute("Element"))
                this.BaseElement = (MM_Element) MM_Serializable.RetrieveConvertedValue(typeof(MM_Element), ElemConfig.Attributes["Element"].Value);
            MM_Serializable.ReadXml(ElemConfig, this);
        }
        #endregion

        #region Transformer drawing
        /// <summary>
        /// Draw the transformer wihtin the parent rectangle.
        /// </summary>
        /// <param name="g"></param>
        public void DrawWinding(Graphics g)
        {
            if (!_Visible)
                return;
            Pen ThisPen = KVLevel == null ? Pens.Red : KVLevel.Energized.ForePen;
            if (IsPhaseShifter)
            {
                Point Center = MM_OneLine_Element.CenterRect(new Rectangle(Point.Empty, Bounds.Size));
                g.DrawEllipse(ThisPen, Center.X - 5, Center.Y - 5, 10, 10);
                using (Pen NewPen = new Pen(ThisPen.Color))
                {
                    NewPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                    g.DrawLine(NewPen, 0, Bounds.Height, Bounds.Width, 0);
                }
            }
            else
                for (int a = 0; a <= 24; a += 6)
                    if (Orientation == MM_OneLine_Element.enumOrientations.Down)
                    {
                        g.DrawLine(ThisPen, a, 3, a, (a == 12 ? 12 : 9));
                        if (a <= 18)
                            g.DrawArc(ThisPen, a, 0, 6, 6, 180, 180);
                    }
                    else if (Orientation == MM_OneLine_Element.enumOrientations.Up)
                    {
                        g.DrawLine(ThisPen, a, (a == 12 ? 0 : 3), a, 9);
                        if (a <= 18)
                            g.DrawArc(ThisPen, a, 6, 6, 6, 0, 180);
                    }
                    else if (Orientation == MM_OneLine_Element.enumOrientations.Left)
                    {

                        g.DrawLine(ThisPen, 3, a, (a == 12 ? 12 : 9), a);
                        if (a <= 18)
                            g.DrawArc(ThisPen, 0, a, 6, 6, 90, 180);
                    }
                    else if (Orientation == MM_OneLine_Element.enumOrientations.Right)
                    {
                        g.DrawLine(ThisPen, (a == 12 ? 0 : 3), a, 9, a);
                        if (a <= 18)
                            g.DrawArc(ThisPen, 6, a, 6, 6, 270, 180);
                    }
        }
        #endregion

        /// <summary>
        /// Write the XML definition for the one-line
        /// </summary>
        /// <param name="xW"></param>
        public void WriteXml(XmlTextWriter xW)
        {
            
            xW.WriteStartElement("Winding");
            MM_Serializable.WriteAttributes(xW, BaseElement, "BaseElement");
            MM_Serializable.WriteXMLData("Orientation", Orientation, xW);
            MM_Serializable.WriteXMLData("Bounds", Bounds, xW);
            MM_Serializable.WriteXMLData("rdfID", rdfID, xW);
            xW.WriteEndElement();
        }

        //TODO: Add winding tostring
    }
}
